import 'package:flutter/material.dart';
import 'package:flutter_music/pages/home/providers/songs_provider.dart';
import 'package:flutter_music/pages/music/widgets/music_cover.dart';
import 'package:flutter_music/pages/music/widgets/music_player.dart';
import 'package:flutter_music/pages/music/widgets/music_progress.dart';
import 'package:flutter_music/utils/audio_player.dart';
import 'package:provider/provider.dart';

class MusicPage extends StatefulWidget {
  const MusicPage({
    super.key,
  });

  @override
  State<MusicPage> createState() => _MusicPageState();
}

class _MusicPageState extends State<MusicPage> {
  @override
  void dispose() {
    AudioPlayerUtils.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Theme.of(context).colorScheme.background,
      appBar: AppBar(
        title: const Text('M U S I C'),
        leading: IconButton(
          onPressed: () => Navigator.pop(context),
          icon: const Icon(Icons.arrow_back),
        ),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.only(bottom: 40),
        child: Consumer<SongsProvider>(
          builder: (context, provider, child) {
            if (provider.currentPlayIndex != null) {
              final song = provider.songs[provider.currentPlayIndex!];
              // 当前音乐总时长
              final Duration total = provider.totalDuration;
              // 当前播放到的位置
              final Duration curPosition = provider.curPositionDuration;
              // 是否正在播放
              final bool isPlaying = provider.isPlaying;

              return Container(
                margin: const EdgeInsets.only(left: 20, right: 20, top: 10),
                child: Column(
                  children: [
                    /// 歌曲封面图片、信息
                    MusicCover(song: song),

                    /// 歌曲操作
                    MusicProgress(
                      total: total,
                      curPosition: curPosition,
                    ),

                    /// 歌曲播放进度
                    SliderTheme(
                      data: SliderTheme.of(context).copyWith(
                        // 取消进度上的圆
                        thumbShape: const RoundSliderThumbShape(enabledThumbRadius: 0),
                      ),
                      child: Slider(
                        min: 0,
                        max: total.inSeconds.toDouble(),
                        value: curPosition.inSeconds.toDouble(),
                        activeColor: Colors.green,
                        inactiveColor: Colors.grey,
                        onChanged: (value) => provider.seek(
                          Duration(seconds: value.toInt()),
                        ),
                      ),
                    ),

                    /// 上一首、暂停/播放、下一首
                    const SizedBox(height: 10),
                    MusicPlayer(
                      isPlaying: isPlaying,
                      onPrevious: () => provider.playPreviousSong(),
                      onCenter: () => provider.playOrPause(),
                      onNext: () => provider.playNextSong(),
                    ),
                  ],
                ),
              );
            } else {
              return const SizedBox.shrink();
            }
          },
        ),
      ),
    );
  }
}
